package cloud.maque.blog.impl;

import cloud.maque.blog.entity.MqCategory;
import cloud.maque.blog.mapper.MqCategoryMapper;
import cloud.maque.blog.service.MqCategoryService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;

/**
 * 分类表;(MQ_CATEGORY)表服务实现类
 *
 * @author : maque.cloud
 * @date : 2025-2-5
 */
@Service
public class MqCategoryServiceImpl extends ServiceImpl<MqCategoryMapper, MqCategory> implements MqCategoryService {
    @Autowired
    private MqCategoryMapper mqCategoryMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param categoryId 主键
     * @return 实例对象
     */
    public MqCategory queryById(String categoryId) {
        return mqCategoryMapper.selectById(categoryId);
    }

    /**
     * 分页查询
     *
     * @param mqCategory 筛选条件
     * @param current    当前页码
     * @param size       每页大小
     * @return
     */
    public IPage<MqCategory> paginQuery(MqCategory mqCategory, long current, long size) {
        //1. 构建动态查询条件
        LambdaQueryWrapper<MqCategory> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(mqCategory.getTenantId())) {
            queryWrapper.eq(MqCategory::getTenantId, mqCategory.getTenantId());
        }
        if (StrUtil.isNotBlank(mqCategory.getCreatedBy())) {
            queryWrapper.eq(MqCategory::getCreatedBy, mqCategory.getCreatedBy());
        }
        if (StrUtil.isNotBlank(mqCategory.getUpdatedBy())) {
            queryWrapper.eq(MqCategory::getUpdatedBy, mqCategory.getUpdatedBy());
        }
        if (StrUtil.isNotBlank(mqCategory.getCategoryName())) {
            queryWrapper.eq(MqCategory::getCategoryName, mqCategory.getCategoryName());
        }
        if (StrUtil.isNotBlank(mqCategory.getParentId())) {
            queryWrapper.eq(MqCategory::getParentId, mqCategory.getParentId());
        }
        if (StrUtil.isNotBlank(mqCategory.getShortName())) {
            queryWrapper.eq(MqCategory::getShortName, mqCategory.getShortName());
        }
        if (StrUtil.isNotBlank(mqCategory.getUrl())) {
            queryWrapper.eq(MqCategory::getUrl, mqCategory.getUrl());
        }
        if (StrUtil.isNotBlank(mqCategory.getIntro())) {
            queryWrapper.eq(MqCategory::getIntro, mqCategory.getIntro());
        }
        if (StrUtil.isNotBlank(mqCategory.getBanner())) {
            queryWrapper.eq(MqCategory::getBanner, mqCategory.getBanner());
        }
        //2. 执行分页查询
        Page<MqCategory> pagin = new Page<>(current, size, true);
        IPage<MqCategory> selectResult = mqCategoryMapper.selectByPage(pagin, queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return this.page(pagin, queryWrapper);
    }


    /**
     * 更新数据
     *
     * @param mqCategory 实例对象
     * @return 实例对象
     */
    public MqCategory Save(MqCategory mqCategory) {

        if (StrUtil.isEmptyIfStr(mqCategory.getCategoryId())) {
            this.baseMapper.insert(mqCategory);
            return mqCategory;
        }

        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<MqCategory> chainWrapper = new LambdaUpdateChainWrapper<MqCategory>(mqCategoryMapper);
        if (StrUtil.isNotBlank(mqCategory.getTenantId())) {
            chainWrapper.eq(MqCategory::getTenantId, mqCategory.getTenantId());
        }
        if (StrUtil.isNotBlank(mqCategory.getCreatedBy())) {
            chainWrapper.eq(MqCategory::getCreatedBy, mqCategory.getCreatedBy());
        }
        if (StrUtil.isNotBlank(mqCategory.getUpdatedBy())) {
            chainWrapper.eq(MqCategory::getUpdatedBy, mqCategory.getUpdatedBy());
        }
        if (StrUtil.isNotBlank(mqCategory.getCategoryName())) {
            chainWrapper.eq(MqCategory::getCategoryName, mqCategory.getCategoryName());
        }
        if (StrUtil.isNotBlank(mqCategory.getParentId())) {
            chainWrapper.eq(MqCategory::getParentId, mqCategory.getParentId());
        }
        if (StrUtil.isNotBlank(mqCategory.getShortName())) {
            chainWrapper.eq(MqCategory::getShortName, mqCategory.getShortName());
        }
        if (StrUtil.isNotBlank(mqCategory.getUrl())) {
            chainWrapper.eq(MqCategory::getUrl, mqCategory.getUrl());
        }
        if (StrUtil.isNotBlank(mqCategory.getIntro())) {
            chainWrapper.eq(MqCategory::getIntro, mqCategory.getIntro());
        }
        if (StrUtil.isNotBlank(mqCategory.getBanner())) {
            chainWrapper.eq(MqCategory::getBanner, mqCategory.getBanner());
        }
        //2. 设置主键，并更新
        chainWrapper.set(MqCategory::getCategoryId, mqCategory.getCategoryId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if (ret) {
            return queryById(mqCategory.getCategoryId());
        } else {
            return mqCategory;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param categoryId 主键
     * @return 是否成功
     */
    public boolean deleteById(String categoryId) {
        int total = mqCategoryMapper.deleteById(categoryId);
        return total > 0;
    }
}